package XML;
import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;




public class ReadXML {

	public static Document BuildXML(String fName)
	{
		 Document doc = null;
		 DocumentBuilder builder = null;
	     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	     try
        {
            builder = factory.newDocumentBuilder();
            doc = builder.newDocument();

        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        
        File f = new File(fName);
        if(f.exists()==true)
        {
        	doc = DOMUltil.parse(fName);
        }
        return doc;
	}
	
	private static String getTextValue(Element e, String tagName) {
		String textVal = null;
		NodeList nl = e.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}
		return textVal;
	}

	/**
	 * Calls getTextValue and returns a int value
	 */
	private static int getIntValue(Element e, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(e,tagName));
	}
	
	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch cÃ¡c ip tá»« 1 file cá»¥ thá»ƒ
	 * @param doc
	 * @param fName
	 * @return
	 */
	public static ArrayList<ArrayList<String>> ReadIP(Document doc, String fName)
	{

		doc = BuildXML(fName);
		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();
		ArrayList<String> learnerList = new ArrayList<String>();
		if(doc!=null)
		{
			  
            NodeList learner = doc.getElementsByTagName("Learner");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		learnerList.add(e.getAttribute("ID"));
            		learnerList.add(e.getAttribute("UserName"));
            		learnerList.add(e.getAttribute("FullName"));
            		learnerList.add(getTextValue(e, "IP"));
            		learnerList.add(e.getAttribute("IDClass"));
            		ipList.add(learnerList);
            	}
            }
           
        }
		return ipList;
	}
	/**
	  * Láº¥y danh sÃ¡ch IP theo username
	  */
	public static ArrayList<String> ReadIP(Document doc, String fName, String sUsername)
	{
		ArrayList<String> arr = new ArrayList<String>();
		if(doc != null)
		{
			NodeList resList = doc.getElementsByTagName("Learner");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				if(e.getAttribute("UserName").equals(sUsername))
				{
				  	arr.add(getTextValue(e, "IP"));
				  	break;
				}
			}	
		}
		return arr;
	}
	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch cÃ¡c ip tá»« 1 document
	 * @param doc
	 * @return
	 */
	public static ArrayList<ArrayList<String>> ReadIP(Document doc)
	{

		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();
		
		if(doc!=null)
		{
			  
            NodeList learner = doc.getElementsByTagName("Learner");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		ArrayList<String> learnerList = new ArrayList<String>();
            		learnerList.add(e.getAttribute("ID"));
            		learnerList.add(e.getAttribute("UserName"));
            		learnerList.add(e.getAttribute("FullName"));
            		learnerList.add(getTextValue(e, "IP"));
            		learnerList.add(e.getAttribute("IDClass"));
            		ipList.add(learnerList);
            	}
            }
           
        }
		
		return ipList;
	}

	public static ArrayList<ArrayList<String>> ReadIP(Document doc, int idclass)
	{
		ArrayList<ArrayList<String>> ipList = new ArrayList<ArrayList<String>>();	
		if(doc!=null)
		{  
            NodeList learner = doc.getElementsByTagName("Learner");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		ArrayList<String> learnerList = new ArrayList<String>();
            		if(Integer.valueOf(e.getAttribute("IDClass")) == idclass)
            		{
	            		learnerList.add(e.getAttribute("ID"));
	            		learnerList.add(e.getAttribute("UserName"));
	            		learnerList.add(e.getAttribute("FullName"));
	            		learnerList.add(getTextValue(e, "IP"));
	            		learnerList.add(e.getAttribute("IDClass"));
	            		ipList.add(learnerList);
            		}
            	}
            }
           
        }
		
		return ipList;
	}
	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch cÃ¡c ip tá»« 1 file cá»¥ thá»ƒ
	 * @param doc
	 * @param fName
	 * @return
	 */
	public static ArrayList<String> ReadCH(Document doc, String fName)
	{

		doc = BuildXML(fName);
		ArrayList<String> arrCH = new ArrayList<String>();
		
		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Info");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		
            		Element e = (Element)learner.item(i);
            		arrCH.add(e.getAttribute("ServerName"));
            		arrCH.add(e.getAttribute("AccServer"));
            		arrCH.add(e.getAttribute("PassServer"));
            		arrCH.add(e.getAttribute("IP"));
            		arrCH.add(e.getAttribute("AccLearner"));
            		arrCH.add(e.getAttribute("PassLearner"));
            		arrCH.add(e.getAttribute("AutoLogin"));
            	}
            }
           
        }
		
		return arrCH;
	}

	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch cÃ¡c ip tá»« 1 document
	 * @param doc
	 * @return
	 */
	public static ArrayList<String> ReadCH(Document doc)
	{

		ArrayList<String> arrCH = new ArrayList<String>();
		
		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Info");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		
            		Element e = (Element)learner.item(i);
            		arrCH.add(e.getAttribute("ServerName"));
            		arrCH.add(e.getAttribute("AccServer"));
            		arrCH.add(e.getAttribute("PassServer"));
            		arrCH.add(e.getAttribute("IP"));
            		arrCH.add(e.getAttribute("AccLearner"));
            		arrCH.add(e.getAttribute("PassLearner"));
            		arrCH.add(e.getAttribute("AutoLogin"));
            	}
            }
           
        }
		
		return arrCH;
	}
	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch cÃ¡c ip tá»« 1 file cá»¥ thá»ƒ
	 * @param doc
	 * @param fName
	 * @return
	 */
	public static ArrayList<String> ReadFB(Document doc, String fName)
	{

		doc = BuildXML(fName);
		ArrayList<String> arrFB = new ArrayList<String>();
		
		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Feedback");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		arrFB.add(e.getAttribute("Title"));
            		arrFB.add(e.getAttribute("IDPart"));
            		arrFB.add(e.getAttribute("IDDetailLearning"));
            		arrFB.add(e.getAttribute("Content"));
            		arrFB.add(e.getAttribute("IDEx"));
            		break;
            	}
            }
           
        }
		
		return arrFB;
	}

	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch cÃ¡c ip tá»« 1 document
	 * @param doc
	 * @return
	 */
	public static ArrayList<String> ReadFB(Document doc)
	{

		ArrayList<String> arrFB = new ArrayList<String>();
		
		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Feedback");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		
            		Element e = (Element)learner.item(i);
            		arrFB.add(e.getAttribute("Title"));
            		arrFB.add(e.getAttribute("IDPart"));
            		arrFB.add(e.getAttribute("IDDetailLearning"));
            		arrFB.add(e.getAttribute("Content"));
            		arrFB.add(e.getAttribute("IDEx"));
            	}
            }
           
        }
		
		return arrFB;
	}
	/**
	 * thong tin cau hinh trong setting
	 * @param doc
	 * @param fName
	 * @return
	 */
	public static ArrayList<String> ReadSetting(Document doc, String fName)
	{

		doc = BuildXML(fName);
		ArrayList<String> arrFB = new ArrayList<String>();
		
		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Info");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		arrFB.add(e.getAttribute("TimeFB"));
            		arrFB.add(e.getAttribute("AutoFB"));
            		arrFB.add(e.getAttribute("TimeFeel"));
            		arrFB.add(e.getAttribute("AutoFeel"));
            		arrFB.add(e.getAttribute("AutoShare"));
            		arrFB.add(e.getAttribute("SaveChat"));
            		break;
            	}
            }
           
        }
		
		return arrFB;
	}

	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch thong tin cau hinh trong setting
	 * @param doc
	 * @return
	 */
	public static ArrayList<String> ReadSetting(Document doc)
	{

		ArrayList<String> arrFB = new ArrayList<String>();
		
		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Info");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{

            		Element e = (Element)learner.item(i);
            		arrFB.add(e.getAttribute("TimeFB"));
            		arrFB.add(e.getAttribute("AutoFB"));
            		arrFB.add(e.getAttribute("TimeFeel"));
            		arrFB.add(e.getAttribute("AutoFeel"));
            		arrFB.add(e.getAttribute("AutoShare"));
            		arrFB.add(e.getAttribute("SaveChat"));
            	}
            }
           
        }
		
		return arrFB;
	}
	
	/**
	 * thong tin cau hinh trong setting
	 * @param doc
	 * @param fName
	 * @return
	 */
	public static int ReadIDMax(Document doc, String fName)
	{

		doc = BuildXML(fName);
		
		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Info");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		return Integer.valueOf(e.getAttribute("ID"));
            	}
            }
        }
		
		return 0;
	}

	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch thong tin cau hinh trong setting
	 * @param doc
	 * @return
	 */
	public static int ReadIDMax(Document doc)
	{

		if(doc!=null)
		{
            NodeList learner = doc.getElementsByTagName("Info");
            int n = learner.getLength();
            if(n!= 0)
            {
            	for(int i=0; i<n; i++)
            	{
            		Element e = (Element)learner.item(i);
            		return Integer.valueOf(e.getAttribute("ID"));
            	}
            }
           
        }
		
		return 0;
	}
	/**
	 * Ä�á»�c danh sÃ¡ch cÃ¢u há»�i tá»« file
	 * @param path
	 * @return
	 */
	public static ArrayList<ArrayList<Object>> ReadQuestion(String path)
	{
		ArrayList<ArrayList<Object>> quesList = new ArrayList<ArrayList<Object>>();
			Document doc = BuildXML(path);
			if(doc!=null)
			{
				NodeList nlQues = doc.getElementsByTagName("Question");
				int n = nlQues.getLength();
				if(n!= 0)
				{
					for(int i=0; i<n; i++)
					{
						Element e = (Element)nlQues.item(i);
						ArrayList<Object> ques = new ArrayList<Object>();
						
						int type = getIntValue(e, "Type");
						ques.add(String.valueOf(type));
						ques.add(getTextValue(e, "Content"));
						
						NodeList nlOpt = e.getElementsByTagName("Option");
						int length = nlOpt.getLength();
						
						for(int j = 0; j < length; j++)
						{
							Element opt= (Element)nlOpt.item(j);
							if(type !=5)
							{
								ques.add(opt.getAttribute("isAnswer"));
							}
							ques.add(opt.getTextContent());
						}
						quesList.add(ques);
					}
				}

			}

			return quesList;
	}

	//-------------------------------------------------------------
	public static ArrayList<Object> ReadGroup(Document doc, String fName, String teamleader)
	{

		doc = BuildXML(fName);
		ArrayList<Object> arrFB = new ArrayList<Object>();
		
		if(doc!=null)
		{
			NodeList resList = doc.getElementsByTagName("Team");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				if(e.getAttribute("TeamLeader").equals(teamleader))
				{
            		arrFB.add(e.getAttribute("TeamLeader")+"");
            		arrFB.add(e.getAttribute("IDDetailLearning"));
            		arrFB.add(e.getAttribute("NameExercises")+"");
            		arrFB.add(e.getAttribute("FileName")+"");
            		arrFB.add(e.getAttribute("Time"));
            		break;
            	}
            }
           
        }
		
		return arrFB;
	}

	/**
	 * HÃ m tráº£ vá»� danh sÃ¡ch thong tin cau hinh trong setting
	 * @param doc
	 * @return
	 */
	public static ArrayList<ArrayList<Object>> ReadGroupByTeam(Document doc, String filename, String teamleader)
	{
		ArrayList<ArrayList<Object>> arr = new ArrayList<ArrayList<Object>>();
		ArrayList<Object> arrFB;
		
		if(doc!=null)
		{
			NodeList resList = doc.getElementsByTagName("Team");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				if(e.getAttribute("TeamLeader").equals(teamleader))
				{
            		NodeList nodelist = e.getElementsByTagName("Info");
            		for(int j = 0; j<nodelist.getLength();j++)
            		{
            			arrFB = new ArrayList<Object>();
            			Element el = (Element)nodelist.item(j);
            			arrFB.add(el.getAttribute("UserName"));
                		arrFB.add(el.getAttribute("FullName"));
                		arrFB.add(el.getAttribute("IP"));
                		arr.add(arrFB);
            		}
            	}
            }
           
        }
		
		return arr;
	}
	
	public static ArrayList<Object> ReadGroupFull(Document doc, String filename)
	{
		ArrayList<Object> arrFB = new ArrayList<Object>();
		
		if(doc!=null)
		{
			NodeList resList = doc.getElementsByTagName("Team");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
        		arrFB.add(e.getAttribute("TeamLeader")+"");
        		arrFB.add(e.getAttribute("IDDetailLearning"));
        		arrFB.add(e.getAttribute("NameExercises")+"");
        		arrFB.add(e.getAttribute("FileName")+"");
        		arrFB.add(e.getAttribute("Time"));
            }
           
        }
		
		return arrFB;
	}
	
	public static ArrayList<Object> ReadGroupTeamlearder(Document doc, String filename)
	{
		ArrayList<Object> arrFB = new ArrayList<Object>();
		
		if(doc!=null)
		{
			NodeList resList = doc.getElementsByTagName("Team");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
        		arrFB.add(e.getAttribute("TeamLeader")+"");
            }
           
        }
		
		return arrFB;
	}
	//moi them vao
	
	/**
	 * hàm trả về danh sách thông tin học viên: CMember
	 * @param doc
	 * @return
	 */
	public static ArrayList<CMember> ReadMember(Document doc)
	{
		ArrayList<CMember> memberList = new ArrayList<CMember>();

		if(doc!=null)
		{
			NodeList nlearner = doc.getElementsByTagName("Learner");
			int n = nlearner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nlearner.item(i);
					//có 3 phần tử: [0]: userName; [1]: ip; [2]: fullname
					ArrayList<String> learner = new ArrayList<String>();
					learner.add(e.getAttribute("UserName"));
					learner.add(getTextValue(e, "IP"));
					learner.add(getTextValue(e, "FullName"));
					
					CMember mem = new  CMember();
					mem.set_fullName(getTextValue(e, "FullName"));
					mem.set_ip(getTextValue(e, "IP"));
					mem.set_userName(e.getAttribute("UserName"));
					memberList.add(mem);
				}
			}

		}

		return memberList;
	}

	public static ArrayList<CMember> ReadMember(String path)
	{
		ArrayList<CMember> memberList = new ArrayList<CMember>();
		Document doc = BuildXML(path);
		if(doc!=null)
		{
			NodeList nlearner = doc.getElementsByTagName("Learner");
			int n = nlearner.getLength();
			if(n!= 0)
			{
				for(int i=0; i<n; i++)
				{
					Element e = (Element)nlearner.item(i);
					//có 3 phần tử: [0]: userName; [1]: ip; [2]: fullname
					ArrayList<String> learner = new ArrayList<String>();
					learner.add(e.getAttribute("UserName"));
					learner.add(getTextValue(e, "IP"));
					learner.add(getTextValue(e, "FullName"));
					
					CMember mem = new  CMember();
					mem.set_fullName(getTextValue(e, "FullName"));
					mem.set_ip(getTextValue(e, "IP"));
					mem.set_userName(e.getAttribute("UserName"));
					memberList.add(mem);
				}
			}

		}

		return memberList;
	}
	
}
